package com.wx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.entity.GoodsContent;
import com.wx.entity.GoodsContentPics;
import com.wx.entity.GoodsReputation;
import com.wx.entity.IndexCategory;
import com.wx.entity.IndexGoods;
import com.wx.entity.WechatUser;
import com.wx.entity.goods.Reputation;
import com.wx.mapper.GoodsContentMapper;
import com.wx.mapper.GoodsContentPicsMapper;
import com.wx.mapper.GoodsReputationMapper;
import com.wx.mapper.IndexCategoryMapper;
import com.wx.mapper.IndexGoodsMapper;
import com.wx.mapper.WechatUserMapper;
import com.wx.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private IndexCategoryMapper indexCategoryMapper;
	
	@Autowired
	private IndexGoodsMapper indexGoodsMapper;
	
	@Autowired
	private GoodsContentMapper goodsContentMapper;
	
	@Autowired
	private GoodsContentPicsMapper goodsContentPicsMapper;
	
	@Autowired
	private GoodsReputationMapper goodsReputationMapper;
	
	@Autowired
	private WechatUserMapper userMapper;
	
	@Override
	public IndexCategory selectByCategoryId(Integer id) {
		if(null != id) {
			return indexCategoryMapper.selectByCategoryId(id);
		}
		return null;
	}

	@Override
	public IndexGoods selectByPrimaryKey(Integer id) {
		return indexGoodsMapper.selectByPrimaryKey(id);
	}

	@Override
	public GoodsContent selectContentByGoodsId(Integer id) {
		return goodsContentMapper.selectByGoodsId(id);
	}

	@Override
	public List<GoodsContentPics> selectContentPicsByGoodsId(Integer id) {
		return goodsContentPicsMapper.selectByGoodsId(id);
	}

	@Override
	public List<Reputation> selectGoodsReputation(Integer id) {
		List<Reputation> list = new ArrayList<Reputation>();
		Reputation reputation = new Reputation();
		
		List<GoodsReputation> reList = goodsReputationMapper.selectByGoodsId(id);
		
		if(reList.size() == 0) {
			return null;
		}
		
		for(GoodsReputation item : reList) {
			reputation.setGoods(item);
			
			WechatUser user = userMapper.selectByPrimaryKey(item.getUid());
			reputation.setUser(user);
			
			list.add(reputation);
		}
		
		return list;
	}

}
